package com.example.springboot_08_restful.service.impl;

import com.example.springboot_08_restful.pojo.Message;
import com.example.springboot_08_restful.service.IMessageService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther cangshufeilun
 * @date 2022/4/3 15:16
 */
@Service
public class MessageServicelmpl implements IMessageService {

    List<Message> list = new ArrayList<>();

    {
        list.add(new Message(1L,"你好","哈哈"));
        list.add(new Message(2L,"你好","哈哈"));
        list.add(new Message(3L,"你好","哈哈"));
    }

    @Override
    public void save(Message m) {
        list.add(m);
    }

    @Override
    public List<Message> findAll() {
        return list;
    }

    @Override
    public Message findById(Long id) {
        for(Message m:list)
        {
            if(m.getId().equals(id))
            {
                return m;
            }
        }
        return null;
    }

    @Override
    public void removeById(Long id) {
        for(Message m:list)
        {
            if(m.getId().equals(id))
            {
                list.remove(m);
                return;
            }
        }
    }

    @Override
    public void updateById(Message m2)
    {
        for(Message m:list)
        {
            if(m.getId().equals(m2.getId()))
            {
                m.setText(m2.getText());
                m.setSummary(m2.getSummary());
                return;
            }
        }

    }

    @Override
    public void updateText(Message m2)
    {
        for(Message m:list)
        {
            if(m.getId().equals(m2.getId()))
            {
                m.setText(m2.getText());
                return;
            }
        }

    }
}
